﻿using EasyAdmin.Application.Contracts.Systems.Users;
using EasyAdmin.Application.Contracts.Systems.Users.Dtos;
using EasyAdmin.Domain.Entities.Systems;
using EasyCore.Application;
using EasyCore.ObjectMapper;
using EasyCore.UnifyResult.Exceptions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Application.Systems
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserAppService : AppService, IUserAppService
    {
        /// <summary>
        /// 用户仓储
        /// </summary>
        private readonly ISqlSugarRepository<SysUser> _userRepo;

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserAppService(ISqlSugarRepository<SysUser> userRepo)
        {
            this._userRepo = userRepo;
        }

        /// <summary>
        /// 用户分页
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<SqlSugarPagedList<UserOutput>> Page(UserPageCondition condition)
        {
            var  data = await _userRepo.AsQueryable().InnerJoin<SysOrg>((a, b) => a.OrgId == b.Id)
                 .Where((a, b) => a.IsDeleted == false && b.IsDeleted == false).Select((a, b) => new UserOutput()
                 {
                     Id = a.Id,
                     Name = a.Name,
                     Gender = a.Gender,
                     Birthday = a.Birthday,
                     Nickname = a.Nickname,
                     Account = a.Account,
                     Avatar = a.Avatar,
                     Signature = a.Signature,
                     OrgId = a.OrgId,
                     PositionId = a.PositionId,
                     IsDisabled = a.IsDisabled,
                     OrgName = b.Name,
                     OrgFullName = b.FullName
                 }).ToPagedListAsync(condition.PageIndex,condition.PageSize);
            return data;
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateUser(CreateUserInput input)
        {
            var user = await _userRepo.FirstOrDefaultAsync(m => m.Account == input.Account && m.IsDeleted == false);
            if (user != null) BusinessThrow.Throw( "账号已存在已存在");
            user = input.MapTo<SysUser>();
            await _userRepo.InsertAsync(user);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteUserByUserId(long userId)
        {
            var user = await _userRepo.FirstOrDefaultAsync(m => m.Id == userId);
            if (user == null) BusinessThrow.Throw("数据不存在");
            await _userRepo.DeleteAsync(user);
        }

        /// <summary>
        /// 获得用户信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<UserOutput> GetUserByUserId(long userId)
        {
            var user = await _userRepo.AsQueryable().InnerJoin<SysOrg>((a, b) => a.OrgId == b.Id)
                 .Where((a, b) => a.IsDeleted == false && b.IsDeleted == false).Select((a, b) => new UserOutput()
                 {
                     Id = a.Id,
                     Name = a.Name,
                     Gender = a.Gender,
                     Birthday = a.Birthday,
                     Nickname = a.Nickname,
                     Account = a.Account,
                     Avatar = a.Avatar,
                     Signature = a.Signature,
                     OrgId = a.OrgId,
                     PositionId = a.PositionId,
                     IsDisabled = a.IsDisabled,
                     OrgName = b.Name,
                     OrgFullName = b.FullName
                 }).FirstAsync();
            return user;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateUser(UpdateUserInput input)
        {
            var user = await _userRepo.FirstOrDefaultAsync(m => m.Id == input.Id);
            if (user == null) BusinessThrow.Throw("数据不存在");
            user = input.MapTo<SysUser>();
            await _userRepo.UpdateAsync(user);
        }
    }
}
